﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_Item
    /// </summary>
    public class DBLoader_Item : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DBLoader_Item(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = db.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select * from ItemResource";
            cmd.CommandType = CommandType.Text;

            int count = 0;
            conn.Open();
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    ItemBaseServer item = new ItemBaseServer();
                    int v = 0;
			        item.nCode = dr.GetInt32(v++);
                    item.nNameId = dr.GetInt32(v++);
                    int tooltip_id = dr.GetInt32(v++);
                    item.nType = dr.GetInt32(v++);
                    item.nGroup = dr.GetInt32(v++);
                    item.nClass = (ItemBase.ItemClass)dr.GetInt32(v++);
                    item.WearType = (ItemBase.ItemWearType)dr.GetInt32(v++);
                    item.nSetID = dr.GetInt32(v++);
                    item.nSetPartFlag = dr.GetInt32(v++);
                    item.nGrade = dr.GetByte(v++);
                    item.nRank = dr.GetInt32(v++);
                    item.nLevel = dr.GetInt32(v++);
                    item.nEnhance = dr.GetInt32(v++);
                    item.nSocketCount = dr.GetInt32(v++);
                    item.nInstanceFlag = (uint)dr.GetInt32(v++);
                    byte limit_deva = dr.GetByte(v++);
                    byte limit_asura = dr.GetByte(v++);
                    byte limit_gaia = dr.GetByte(v++);
                    byte limit_fighter = dr.GetByte(v++);
                    byte limit_job_depth = dr.GetByte(v++);
                    byte limit_hunter = dr.GetByte(v++);
                    byte limit_magician = dr.GetByte(v++);
                    byte limit_summoner = dr.GetByte(v++);

                    item.nLimit = 0;
                    if (limit_asura != 0)
                        item.nLimit = 8;
                    if (limit_gaia != 0)
                        item.nLimit |= 0x10;
                    if (limit_deva != 0)
                        item.nLimit |= 4;
                    if (limit_hunter != 0)
                        item.nLimit |= 0x800;
                    if (limit_fighter != 0)
                        item.nLimit |= 0x400;
                    if (limit_magician != 0)
                        item.nLimit |= 0x1000;
                    if (limit_summoner != 0)
                        item.nLimit |= 0x2000;
                    item.nMinLevel = dr.GetInt32(v++);
                    item.nMaxLevel = dr.GetInt32(v++);
                    item.nTargetMinLevel = dr.GetInt32(v++);
                    item.nTargetMaxLevel = dr.GetInt32(v++);
                    float range = (float)dr.GetDecimal(v++);
                    item.nRange = (int)(range * 100);
                    item.fWeight = (float)dr.GetDecimal(v++);
                    item.nPrice = dr.GetInt32(v++);
                    item.nHuntaholicPoint = dr.GetInt32(v++);
                    item.nDurability = dr.GetInt32(v++);
                    item.nEndurance = dr.GetInt32(v++);
                    item.nMaterial = dr.GetInt32(v++);
                    item.nSummonId = dr.GetInt32(v++);
                    item.Flag = (uint)dr.GetInt32(v++);
                    item.available_time = dr.GetInt32(v++);
                    item.decrease_type = (sbyte)dr.GetByte(v++);
                    item.nThrowRange = (int)dr.GetDecimal(v++);
                    item.nBaseType[0] = dr.GetInt16(v++);
                    item.fBaseVar1[0] = (float)dr.GetDecimal(v++);
                    item.fBaseVar2[0] = (float)dr.GetDecimal(v++);
                    item.nBaseType[1] = dr.GetInt16(v++);
                    item.fBaseVar1[1] = (float)dr.GetDecimal(v++);
                    item.fBaseVar2[1] = (float)dr.GetDecimal(v++);
                    item.nBaseType[2] = dr.GetInt16(v++);
                    item.fBaseVar1[2] = (float)dr.GetDecimal(v++);
                    item.fBaseVar2[2] = (float)dr.GetDecimal(v++);
                    item.nBaseType[3] = dr.GetInt16(v++);
                    item.fBaseVar1[3] = (float)dr.GetDecimal(v++);
                    item.fBaseVar2[3] = (float)dr.GetDecimal(v++);
                    item.nOptType[0] = dr.GetInt16(v++);
                    item.fOptVar1[0] = (float)dr.GetDecimal(v++);
                    item.fOptVar2[0] = (float)dr.GetDecimal(v++);
                    item.nOptType[1] = dr.GetInt16(v++);
                    item.fOptVar1[1] = (float)dr.GetDecimal(v++);
                    item.fOptVar2[1] = (float)dr.GetDecimal(v++);
                    item.nOptType[2] = dr.GetInt16(v++);
                    item.fOptVar1[2] = (float)dr.GetDecimal(v++);
                    item.fOptVar2[2] = (float)dr.GetDecimal(v++);
                    item.nOptType[3] = dr.GetInt16(v++);
                    item.fOptVar1[3] = (float)dr.GetDecimal(v++);
                    item.fOptVar2[3] = (float)dr.GetDecimal(v++);
                    int effect_id = dr.GetInt32(v++);
                    item.nEnhanceID[0] = dr.GetInt16(v++);
                    item.fEnhanceVar[0, 0] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[1, 0] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[2, 0] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[3, 0] = (float)dr.GetDecimal(v++);
                    item.nEnhanceID[1] = dr.GetInt16(v++);
                    item.fEnhanceVar[0, 1] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[1, 1] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[2, 1] = (float)dr.GetDecimal(v++);
                    item.fEnhanceVar[3, 1] = (float)dr.GetDecimal(v++);
                    item.nSkillID = dr.GetInt32(v++);
                    item.nStateCode = dr.GetInt32(v++);
                    item.nStateLevel = dr.GetInt32(v++);
                    item.nStateTime = dr.GetInt32(v++);
                    item.nCoolTime = dr.GetInt32(v++);
                    item.nCoolTimeGroup = dr.GetInt16(v++);
                    int model_type_dem = dr.GetInt32(v++);
                    int model_type_def = dr.GetInt32(v++);
                    int model_type_asm = dr.GetInt32(v++);
                    int model_type_asf = dr.GetInt32(v++);
                    int model_type_gam = dr.GetInt32(v++);
                    int model_type_gaf = dr.GetInt32(v++);
                    int deco_model_change = dr.GetByte(v++);
                    string model_00 = dr.GetString(v++);
                    string model_01 = dr.GetString(v++);
                    string model_02 = dr.GetString(v++);
                    string model_03 = dr.GetString(v++);
                    string model_04 = dr.GetString(v++);
                    string model_05 = dr.GetString(v++);
                    string model_06 = dr.GetString(v++);
                    string model_07 = dr.GetString(v++);
                    string model_08 = dr.GetString(v++);
                    string model_09 = dr.GetString(v++);
                    string model_10 = dr.GetString(v++);
                    string model_11 = dr.GetString(v++);
                    string model_12 = dr.GetString(v++);
                    string model_13 = dr.GetString(v++);
                    string model_14 = dr.GetString(v++);
                    string model_15 = dr.GetString(v++);
                    string model_16 = dr.GetString(v++);
                    string model_17 = dr.GetString(v++);
                    string texture_filename = dr.GetString(v++);
                    string drop_type = dr.GetString(v++);
                    int icon_id = dr.GetInt32(v++);
                    string icon_file_name = dr.GetString(v++);
                    item.strScript = dr.GetString(v++);

                    for (int i = 0; i < ItemBase.MAX_OPTION_NUMBER; ++i)
                    {
                        if (item.nBaseType[i] == 3)
                            item.Flag |= 0x100;
                        if (item.nOptType[i] == 3)
                            item.Flag |= 0x100;
                    }
                    Item.RegisterItemBase(item);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load ItemResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Item Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} Items loaded from ItemResource", count);
            return true;
        }
    }

}
